/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ywb.edugui.view.subject;

import com.ywb.edugui.controller.ControllerFactory;
import com.ywb.edugui.controller.SubjectController;
import com.ywb.edugui.dao.SubjectDao;
import com.ywb.edugui.dao.facotry.DaoFactory;
import com.ywb.edugui.entity.Subject;
import com.ywb.edugui.entity.vo.FirstSubject;
import com.ywb.edugui.view.MainView;
import org.apache.poi.ss.formula.functions.Even;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.*;
import java.awt.*;
import java.util.Enumeration;
import java.util.List;
/**
 *
 * @author 10147
 */
public class SubjectListPanel extends javax.swing.JPanel {
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree;
    // End of variables declaration

    private SubjectController subjectController = ControllerFactory.getSubjectController(this);
    /**
     * Creates new form SubjectListPanel
     */
    public SubjectListPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree = new javax.swing.JTree();
        flushJTreeNode();
        // 设置对节点可以多选，用于批量删除
        jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

        DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)jTree.getCellRenderer();
        cellRenderer.setTextNonSelectionColor(Color.black);
        cellRenderer.setTextSelectionColor(Color.blue);
        //        屏蔽JTree默认的双击展开、折叠效果
        jTree.setToggleClickCount(0);
        //        默认展开节点
        setLayout(null);
        jTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTreeMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTree);

        add(jScrollPane1);
        jScrollPane1.setBounds(30, 30, 370, 320);
    }// </editor-fold>

    private void jTreeMouseClicked(java.awt.event.MouseEvent evt) {
//        鼠标双击时根据点击的节点名称打开科目查询窗口
        if(evt.getClickCount() != 2)
            return;
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
        subjectController.querySubject(selectedNode.toString());
        flushJTreeNode();
    }
//    刷新树节点,当更新/删除后调用
    public void flushJTreeNode(){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();

        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        //        根据数据模型创建JTree
        List<FirstSubject> list = subjectController.getFirstSubjectList();
        //        遍历firstSubjectList把所有节点加入树中
        list.forEach(subject -> {
            DefaultMutableTreeNode parent = new DefaultMutableTreeNode(subject.getTitle());
            subject.getChildren().forEach(secondSubject->{
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(secondSubject.getTitle());
                parent.add(child);
            });

            treeModel.insertNodeInto(parent, root, root.getChildCount());

        });
        jTree.setModel(treeModel);
        expandAll(jTree, new TreePath(root), true);

    }
    // 展开或折叠树的所有节点
    private static void expandAll(JTree tree, TreePath parent, boolean expand) {
//        递归树下的所有节点，展开或者是折叠
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
}
